package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MenuDao;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private MenuDao menuDao;

    @Override
    public void deleteRoleById(int roleId) {

        //根据roleId查询是否与用户关联

        List<Integer> userIds = roleDao.findUserByRoleId(roleId);

        if (userIds != null && userIds.size()>0 ) {
            throw new RuntimeException("该角色正在被使用中，不能删除");
        }

        List<Integer> permissionIds = roleDao.findPermissionByRoleId(roleId);

        if (permissionIds != null) {
            roleDao.delPermission(roleId);
        }

        List<Integer> menuIds = roleDao.findMenuByRoleId(roleId);

        if (menuIds != null) {
            roleDao.delMenu(roleId);
        }

        roleDao.delRole(roleId);

    }


    @Override
    public void edit(Map<String, Object> mapdata) {

        //添加表单数据  回显id
        Role role = (Role) mapdata.get("role");
        List<Integer> permissionIds = (List<Integer>) mapdata.get("permissionIds");//权限id集合
        List<Integer> menuIds = (List<Integer>) mapdata.get("menuIds");//主菜单id集合
        List<Integer> menudetailsIds = (List<Integer>) mapdata.get("menudetailsIds");//子菜单id集合

        roleDao.edit(role);
        roleDao.delPermission(role.getId());
        roleDao.delMenu(role.getId());

        if (permissionIds != null && permissionIds.size() > 0) {

            for (Integer permissionId : permissionIds) {
                permissionDao.set(role.getId(), permissionId);
            }
        }

        for (Integer menudetailsId : menudetailsIds) {
            int count = 0;
            //以防止 添加了子菜单没有选择主菜单  如果有这个情况 默认一起关联主菜单
            int parentMenuId = menuDao.findparentMenuIdById(menudetailsId);
            if(menuIds==null || menuIds.size()==0){
                menuIds.add(parentMenuId);
            }else {
                //一个字id查询出的parentMenuId 和主菜单id 逐个判断
                for (Integer menuId : menuIds) {
                    if (parentMenuId == menuId) {
                        count++;
                    }
                }
            }
            //判断完 此子菜单 没有相关对应的父菜单 就默认添加这个子菜单对应的父菜单
            if(count==0){
                menuIds.add(parentMenuId);
            }

        }



        for (Integer menuId : menuIds) {
            //维护菜单表数据 这里添加的是主菜单id
            menuDao.set(role.getId(), menuId);
        }


        if (menudetailsIds != null && menudetailsIds.size()>0) {
            for (Integer menudetailsId : menudetailsIds) {
                //维护菜单表数据 这里添加的是 子菜单id
                menuDao.set(role.getId(), menudetailsId);
            }
        }
    }
    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //开始分页  传入当前页  当前页放的元素个数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Role> rolePage = roleDao.findPage(queryPageBean.getQueryString());
        return new PageResult(rolePage.getTotal(),rolePage);
    }

    //执行添加操作
    @Override
    public void add(Map<String,Object> mapdata) {
        //添加表单数据  回显id
        Role role = (Role) mapdata.get("role");
        roleDao.add(role);
        List<Integer> permissionIds=(List<Integer>)mapdata.get("permissionIds");//权限id集合
        List<Integer> menuIds=(List<Integer>)mapdata.get("menuIds");//主菜单id集合
        List<Integer> menudetailsIds=(List<Integer>)mapdata.get("menudetailsIds");//子菜单id集合

        if(role.getId()!=null && permissionIds!=null && permissionIds.size()>0 && menudetailsIds!=null &&menudetailsIds.size()>0){

            for (Integer menudetailsId : menudetailsIds) {
                int count = 0;
                //以防止 添加了子菜单没有选择主菜单  如果有这个情况 默认一起关联主菜单
                int parentMenuId = menuDao.findparentMenuIdById(menudetailsId);
                if(menuIds==null || menuIds.size()==0){
                    menuIds.add(parentMenuId);
                }else {
                    //一个字id查询出的parentMenuId 和主菜单id 逐个判断
                    for (Integer menuId : menuIds) {
                        if (parentMenuId == menuId) {
                            count++;
                        }
                    }
                }
                //判断完 此子菜单 没有相关对应的父菜单 就默认添加这个子菜单对应的父菜单
                if(count==0){
                    menuIds.add(parentMenuId);
                }

            }

            for (Integer permissionId : permissionIds) {
                //维护权限表数据
                permissionDao.set(role.getId(),permissionId);
            }
            for (Integer menuId : menuIds) {
                //维护菜单表数据 这里添加的是主菜单id
                menuDao.set(role.getId(),menuId);
            }
            for (Integer menudetailsId : menudetailsIds) {
                //维护菜单表数据 这里添加的是 子菜单id
                menuDao.set(role.getId(),menudetailsId);
            }

        }

    }

    @Override
    public Map<String, Object> findRoleById(int id) {

        Role role = roleDao.findRoleDetailById(id);

        Set<Permission> permissions = permissionDao.findPermissionsByRoleId(id);

        List<Integer> permissionIds = new ArrayList<>();

        for (Permission permission : permissions) {
            permissionIds.add(permission.getId());
        }

        List<Integer> menuIds = menuDao.findMenuByRoleId(id);

        List<Menu> firstMenus = menuDao.findFirstMenus();
        List<Integer> firstMenusIds = new ArrayList<>();

        for (Menu firstMenu : firstMenus) {
            firstMenusIds.add(firstMenu.getId());
        }

        List<Integer> parentMenuIds = new ArrayList<>();
        List<Integer> childMenuIds = new ArrayList<>();
        for (Integer menuId : menuIds) {
            if (firstMenusIds.contains(menuId)) {
                parentMenuIds.add(menuId);
            } else {
                childMenuIds.add(menuId);
            }
        }

        Map<String, Object> map = new HashMap<>();

        map.put("formdata", role);
        map.put("menuIds", parentMenuIds);
        map.put("menudetailsIds", childMenuIds);
        map.put("permissionIds", permissionIds);

        return map;
    }
}
